package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
/**
 * 
 * @author msjykf
 * 邮件批次处理
    select eb.state,eb.procstate,eb.batchstate,eb.batch_id,eb.template_id,eb.quantitysent,dt.c_taskcode,dt.saletype,dt.email_distinct
    from email_send_batch eb
    left join doctemplate dt on eb.template_id=dt.doctemplate_id where state&lt;4 and createdate>trunc(sysdate)
 *
 */
public class EmailBatchTask implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(EmailBatchTask.class);
	private String charset = "utf-8"; //字符集
	private int timeout = 100000;//请求超时时间
	private int enableSSLflag = 0;
	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String body){
		int taskstate = map.get("state");  //任务状态
		int procstate = map.get("procstate");  //处理状态
		int batchstate = map.get("batchstate"); //批次状态
		int quantitysent = map.get("quantitysent"); //已发送数量
		def email_distinct = map.get("email_distinct");//是否需要去重
		def batch_id = map.get("batch_id"); //批次号
		def template_id = map.get("template_id"); //模板ID
		def c_taskcode = map.get("c_taskcode"); //调度任务ID
		def saletype = map.get("saletype",""); //销售类型
		
		log.debug("taskstate:"+taskstate+"\n"+"procstate:"+procstate+"\n"+"batchstate:"+batchstate);
		def interfacename,interfaceflag,exclusions,smap;
		int i=0,k=0;
		def tbarray =['email_send_task','email_send_batch'] as String[];
		if(c_taskcode.equals("0001") || c_taskcode.equals("0005")) {
			//0001-电子生日贺卡;0005-开户确认书
			if(batchstate == -2) {
				tbarray = ['email_custinfo','email_custinfo_his','email_send_task','email_send_batch'] as String[];
			}else {
				tbarray = ['email_custinfo'] as String[];
			}	
		}else if (c_taskcode.equals("0002")) {
			//0002-短期理财到期日通知
			if(batchstate == -2) {
				tbarray = ['email_custinfo','email_custinfo_his','email_st_maturity','email_st_maturity_his','email_send_task','email_send_batch'] as String[];
			}else {
				tbarray = ['email_custinfo','email_st_maturity'] as String[];
			}
		}else if (c_taskcode.equals("0003")) {
			//0003-基金分红通知
			if(batchstate == -2) {
				tbarray = ['email_custinfo','email_custinfo_his','email_funddiv_confirm','email_funddiv_confirm_his','email_send_task','email_send_batch'] as String[];
			}else {
				tbarray = ['email_custinfo','email_funddiv_confirm'] as String[];
			}
		}else if (c_taskcode.equals("0004")) {
			//0004-基金交易确认
			if(batchstate == -2) {
				tbarray = ['email_custinfo','email_custinfo_his','email_fundtrade_confirm','email_fundtrade_confirm_his','email_send_task','email_send_batch'] as String[];
			}else {
				tbarray = ['email_custinfo','email_fundtrade_confirm'] as String[];
			}
		}else if (c_taskcode.equals("0006")) {
			//0006-月度对账单/机构客户对账单
			if(batchstate == -2) {
				tbarray = ['email_custinfo','email_custinfo_his','email_fundbalanceinfo','email_fundbalanceinfo_his','email_fundtradeinfo','email_fundtradeinfo_his','email_funddivinfo','email_funddivinfo_his','email_send_task','email_send_batch'] as String[];
			}else {
				tbarray = ['email_custinfo','email_fundbalanceinfo','email_fundtradeinfo','email_funddivinfo'] as String[];
			}	
		}else if (c_taskcode.equals("")) {
			//others
			if(batchstate == -2) {
				tbarray = ['email_send_task','email_send_batch'] as String[];
			}else {
				tbarray = ['email_send_task'] as String[];
			}	
		};
		k = tbarray.length;
		if(batchstate == -1) {
			log.debug("==========="+batch_id+"暂停批次，不处理后续进度================");
			return;
		}else if (batchstate == -2 && taskstate>0) {
			log.debug("==========="+batch_id+"废弃批次处理，数据删除================");
			String[] data=new String[k];
			for(i=0; i<k; i++) {
				data[i] = "delete from "+tbarray[i]+" where batch_id='"+batch_id+"'";
			};
			int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			return;
		};
		PropertiesUtil properties = new PropertiesUtil();
		def ccserverip = properties.readValue("ccserverip"); //获取CallCenter访问地址
		int findrow= 0;
	
		if(taskstate==-1) {
			interfacename = "EmailBatchRequest";
			interfaceflag = 1;
		}else if(taskstate==0){
			log.debug("==========="+batch_id+"等待安正数据返回================");
			return;
		}else if (taskstate==1 && procstate==0) {
			//20190620短信批次增加安正数据回写完成时间节点
			eliteBean.update("update email_send_batch set completedate=sysdate where batch_id='"+batch_id+"'","dataSource");
			
			log.debug("==========="+email_distinct+"邮箱去重================");
	       
			if(email_distinct.equals("1")){
			smap = eliteBean.chaxun_sql(" select email,max(sk_invpty) as sk_invpty,count(*) from email_custinfo where batch_id='"+batch_id+"' group by email having(count(*))>1","dataSource");
			for(i=0; i<smap.size();i++) {
				eliteBean.update("delete from email_custinfo where batch_id='"+batch_id+"' and email='"+smap[i].get("email")+"' and sk_invpty<>"+smap[i].get("sk_invpty")+"","dataSource");
			};
			};
			log.debug("==========="+batch_id+"批次黑名单过滤================");
			if(saletype) {
				exclusions = "select sk_invpty from specialphone where blacktype='EMAIL' and status=1 and sk_invpty is not null and saletype in(0,"+saletype+")";
			}else {
				exclusions = "select sk_invpty from specialphone where blacktype='EMAIL' and status=1 and sk_invpty is not null and saletype=0";
				
			};
			String[] data=new String[k+1];
			for(i=0; i<k; i++) {
				data[i] = "delete from "+tbarray[i]+" where batch_id='"+batch_id+"' and sk_invpty in("+exclusions+")";
			};
			data[k] = "update email_send_batch set procstate=1 where batch_id='"+batch_id+"'";
			int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			return;
		}else if (taskstate==1 && procstate==1) {
			smap = eliteBean.chaxun_sql("select count(*) as findrow from email_custinfo where batch_id='"+batch_id+"'","dataSource");
			findrow = smap[0].get("findrow");
			if(findrow>0) {
				return
			};
			eliteBean.update("update email_send_batch set state=3,procstate=2 where batch_id='"+batch_id+"'","dataSource");
			if(c_taskcode.equals("0001")) {
				def birthdaysmsid = properties.readValue("birthdaysmsid"); //生日祝福短信模板ID
				eliteBean.update("update sms_send_batch set state=1,acceptdate=sysdate,comments='执行成功' where sms_id='"+birthdaysmsid+"' and state=0 and createdate>trunc(sysdate)","dataSource");
			};
		}else if (taskstate==3 && quantitysent>0) {
			smap = eliteBean.chaxun_sql("select count(*) as findrow from email_send_task where batch_id='"+batch_id+"'","dataSource");
			findrow = smap[0].get("findrow");
			if(findrow>0) {
				return
			};
			log.debug("==========="+batch_id+"批次数据发送完成================");
			eliteBean.update("update email_send_batch set state=4 where batch_id='"+batch_id+"'","dataSource");
		};
		if(interfaceflag ==1) {
			def url = "http://"+ccserverip+"/EliteGeneralHttpTask/GeneralService/"+interfacename;
			def jsonbody = "{\"batch_id\":\""+batch_id+"\",\"template_id\":\""+template_id+"\",\"requesttype\": 1}";
			Util.doHttpPost(url,jsonbody,charset,timeout,enableSSLflag);
		};
	}
}